//
//  RCServerLoginVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-26.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCServerLoginVC.h"
#import "AppDelegate.h"
#import "UIImageView+Extented.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "IManager+LoginRequest.h"
#import "RCLoginDataModel.h"
#import "GloableEngineService.h"
#import "NSString+NSString_Ex_.h"

@interface RCServerLoginVC ()<IManagerDelegate,UITextFieldDelegate>{
    UITextField *_nameInputField;
    UITextField *_passInputField;
    int64_t     _reqID;
    UIButton    *_submitBtn;
}

@end

@implementation RCServerLoginVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIImageView *loginBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 0, 0)];
    UIImage *IMG = [UIImage imageNamed:@"bg_serverLogin"];
    [loginBG autoResizeToSetImage:IMG];
    [self.viewContainer addSubview:loginBG];
    [loginBG release];
    
    
    UIImageView *logoBG = [[UIImageView alloc] initWithFrame:CGRectMake(40, 70, 0, 0)];
    UIImage *logoImg = [UIImage imageNamed:@"bg_serverLoginLogo"];
    [logoBG autoResizeToSetImage:logoImg];
    [self.viewContainer addSubview:logoBG];
    [logoBG release];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 210, 70, 20)];
    [self.viewContainer addSubview:nameLabel];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    nameLabel.textColor = kNormalTextColor;
    nameLabel.text =@"商户账号:";
    [nameLabel release];
    
    UIImage *filedImg = [UIImage imageNamed:@"bg_serverLoginField"];

    UIImageView *nameFieldBG = [[UIImageView alloc] initWithFrame:CGRectMake(80, 200, 0, 0)];
    [nameFieldBG autoResizeToSetImage:filedImg];
    [self.viewContainer addSubview:nameFieldBG];
    [nameFieldBG release];
    
    CGRect nameInputFrame = nameFieldBG.frame;
    nameInputFrame.origin.x = nameInputFrame.origin.x + 10;
    nameInputFrame.size.width = nameInputFrame.size.width - 10;
    _nameInputField = [[UITextField alloc] initWithFrame:nameInputFrame];
    _nameInputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nameInputField.delegate = self;
    _nameInputField.returnKeyType = UIReturnKeyDone;
    [self.viewContainer addSubview:_nameInputField];
    _nameInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_nameInputField release];
    
    UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 250, 70, 20)];
    [self.viewContainer addSubview:passwordLabel];
    passwordLabel.backgroundColor = [UIColor clearColor];
    passwordLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    passwordLabel.textColor = kNormalTextColor;
    passwordLabel.text =@"密码:";
    [passwordLabel release];
    
    UIImageView *passFieldBG = [[UIImageView alloc] initWithFrame:CGRectMake(80, 240, 0, 0)];
    [passFieldBG autoResizeToSetImage:filedImg];
    [self.viewContainer addSubview:passFieldBG];
    [passFieldBG release];
    
    CGRect passInputFrame = passFieldBG.frame;
    passInputFrame.origin.x = passInputFrame.origin.x + 10;
    passInputFrame.size.width = passInputFrame.size.width - 10;
    _passInputField = [[UITextField alloc] initWithFrame:passInputFrame];
    _passInputField.secureTextEntry = YES;
    _passInputField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    _passInputField.delegate = self;
    _passInputField.returnKeyType = UIReturnKeyDone;
    [self.viewContainer addSubview:_passInputField];
    _passInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_passInputField release];
    
    _submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(14, 310, 320-28, 40)];
    [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.viewContainer addSubview:_submitBtn];
    UIImage *loginIMG = [UIImage imageNamed:@"login_btn"];
    [_submitBtn setBackgroundImage:loginIMG forState:UIControlStateNormal];
    [_submitBtn addTarget:self action:@selector(submitPress) forControlEvents:UIControlEventTouchUpInside];
    [_submitBtn release];
    
    
    UILabel *notice = [[UILabel alloc] initWithFrame:CGRectMake(0, UIScreenHeight-80, 320, 15)];
    notice.text = @"版权所有 山东泰安如意行旅游开发有限公司";
    notice.backgroundColor = [UIColor clearColor];
    notice.font = [UIFont fontWithName:kFontNameHeiti size:12];
    notice.textColor = kNormalHeadTextColor;
    notice.textAlignment = NSTextAlignmentCenter;
    [self.viewContainer addSubview:notice];
    [notice release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) submitPress
{
    NSString *userName = _nameInputField.text;
    NSString *passWord = _passInputField.text;

    if ([NSString stringIsNullOrEmpty:userName]
        || [NSString stringIsNullOrEmpty:passWord]) {
        [RCAppDelegate alertWithCustomizeString:@"请完整填写登陆信息"];
        return;
    }
    _reqID = [[IManager sharedInstance] requestServerLogin:userName withPassword:passWord withDelegate:self];
}

- (void) loginSuccess
{
    [RCAppDelegate serverLoginSuccess];
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"登录失败"];
   
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    RCServerLoginDataModel *model = (RCServerLoginDataModel *)dataModel;
    if (model.errorNO == RCErrorTypeNoError) {
        [[GloableEngineService sharedInstance] saveServerUserLoginInfo:model];
        [self loginSuccess];
        _passInputField.text = @"";
//        [_delegate loginSuccess];
    }else{
        [RCAppDelegate alertWithCustomizeString:model.errorMsg];
    }
}

#pragma mark-
#pragma mark UITextFieldDelegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}


#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    
    CGFloat keyboardOriginY = UIScreenHeight-20-keyboardRect.size.height;
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0f) {
        keyboardOriginY = keyboardRect.origin.y;
    }
    CGFloat buttonMaxY = CGRectGetMaxY(_submitBtn.frame);
    CGFloat viewcontainOriginY = self.viewContainer.frame.origin.y;
    if (buttonMaxY + viewcontainOriginY + 10 > keyboardOriginY) {
        
        [UIView animateWithDuration:0.3f animations:^{
            CGFloat heightReduce = abs(buttonMaxY + viewcontainOriginY +10 - keyboardOriginY);
            CGRect rect = self.viewContainer.frame;
            rect.origin.y -= heightReduce;
            self.viewContainer.frame = rect;
        }];
        
    }
    
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3f animations:^{
        self.viewContainer.frame = CGRectMake(0, 0, 320, UIScreenHeight-20);
    }];
}
@end
